package com.demo2do.fms.admin.entity.search.business;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;

import com.demo2do.core.persistence.support.QueryBuilder;
import com.demo2do.core.persistence.support.QueryFilter;
import com.demo2do.core.persistence.support.QueryOrder;

/**
 * 查询派车单/维护派车单
 * 
 * @author Chang Liu
 * 
 */
public class DispatchOrderSearch implements QueryBuilder{
    
    /** 所选择公司 */
    private List<Long> organizationIds;
    
    /** 派车单号 */
    private String dispatchOrderCode;
    
    /** 驳运车号 */
    private Long vehicleId;
    
    /** 安吉调度指令组号 */
    private String shipmentGroupId;
    
    /** 起始地 装卸地 */
    private Long startFacilityId;
    
    /** 目的地 装卸地 */
    private Long destinationFacilityId;
    
    /** 派车单日期  */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date beginDispatchDate;
    
    /** 派车单日期 */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date endDispatchDate;
    
    /** 报销开始日期 - 查询派车单用  */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date beginExpenseReimburseDate;
    
    /** 报销结束日期 - 查询派车单用  */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date endExpenseReimburseDate;
    
    /** 预计出发最早日期 - 维护派车单用 */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date beginLeavingDate;
    
    /** 预计出发最晚日期 - 维护派车单用  */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date endLeavingDate;
    
    /** 主驾驶员 */
    private Long mainDriverId;
    
    /** 第一副驾驶员 */
    private Long firstViceDriverId;
    
    /** 
     * 是否已报销
     *  
     * 		- 查询派车单， 选择框
     * 		- 维护派车单， IS_EXPENSE_REIMBURSED = 0
     */
    private Boolean expenseReimbursed;
    
    // ========= 排序 ===========
    /** 派车单号排序 */
    private String codeSort;
    
    /** 预计出发时间排序 */
    private String leavingTimeSort;
    
    /** 派车时间排序 */
    private String dispatchDateSort;
    
    /** 调度指令组号排序 */
    private String shipmentGroupIdSort;
    
    private QueryOrder order;
    
    private QueryFilter filter;
    
    // ======== 查询类别 ============
    private String target;

    /**
     * the default constructor
     */
    public DispatchOrderSearch() {
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getPreparedFilter(boolean)
     */
    public QueryFilter getPreparedFilter(boolean refresh) {
        
        if(filter == null || refresh) {

            filter = new QueryFilter().addEquals("code", dispatchOrderCode)
                                       .addEquals("vehicleId", vehicleId)
                                       .addEquals("shipmentGroupId", shipmentGroupId)
                                       .addEquals("startFacilityId", startFacilityId)
                                       .addEquals("destinationFacilityId", destinationFacilityId)
                                       .addDateTimeBetween("dispatchDate", beginDispatchDate, endDispatchDate)
                                       .addDateTimeBetween("expenseReimburseDate", beginExpenseReimburseDate, endExpenseReimburseDate)
                                       .addDateTimeBetween("leavingTime", beginLeavingDate, endLeavingDate)
                                       .addEquals("mainDriverId", mainDriverId)
                                       .addEquals("firstViceDriverId", firstViceDriverId)
                                       .addSentence("organizationId IN (:organizationIds)", "organizationIds", organizationIds)
        							   .addEquals("expenseReimbursed", expenseReimbursed);
            
        }
        
        return filter;
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getOrder()
     */
    public QueryOrder getOrder() {
        return order;
    }
    
    /**
     * @param refresh
     * @return
     */
    public QueryOrder getPreparedOrder(boolean refresh) {
        
        if (order == null || refresh) {
            
            order = new QueryOrder();
            
            if (StringUtils.isNotBlank(codeSort)) {
                
                order.add("code", codeSort);
                
            } else if (StringUtils.isNotBlank(dispatchDateSort)) {
                
                order.add("dispatchDate", dispatchDateSort);
                
            } else if (StringUtils.isNotBlank(shipmentGroupIdSort)) {
                
                order.add("shipmentGroupId", shipmentGroupIdSort);
                
            } else if (StringUtils.isNotBlank(leavingTimeSort)) {
                
                order.add("leavingTime", leavingTimeSort);
                
            } else {
                
                if (StringUtils.equals("maintain", target)) {
                    order.add("leavingTime", "DESC");
                } else if (StringUtils.equals("search", target)) {
                    order.add("code", "DESC");
                }
                
            }
            
        }
        
        return order;
        
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildSentenceTemplate()
     */
    public String buildSentenceTemplate() {
        String sentence = "FROM DispatchOrder WHERE disabled = false" + getPreparedFilter(false).clauseSentence() + getPreparedOrder(false).sentence();
        return sentence;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
     */
    public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).parameters();
    }
    
    /**
     * @return
     */
    public QueryFilter getFilter(){
        return getPreparedFilter(false);
    }

    /**
     * @return the organizationIds
     */
    public List<Long> getOrganizationIds() {
        return organizationIds;
    }

    /**
     * @return the dispatchOrderCode
     */
    public String getDispatchOrderCode() {
        return dispatchOrderCode;
    }

    /**
     * @return the vehicleId
     */
    public Long getVehicleId() {
        return vehicleId;
    }

    /**
     * @return the shipmentGroupId
     */
    public String getShipmentGroupId() {
        return shipmentGroupId;
    }

    /**
     * @return the startFacilityId
     */
    public Long getStartFacilityId() {
        return startFacilityId;
    }

    /**
     * @return the destinationFacilityId
     */
    public Long getDestinationFacilityId() {
        return destinationFacilityId;
    }

    /**
     * @return the beginDispatchDate
     */
    public Date getBeginDispatchDate() {
        return beginDispatchDate;
    }

    /**
     * @return the endDispatchDate
     */
    public Date getEndDispatchDate() {
        return endDispatchDate;
    }

    /**
     * @return the beginExpenseReimburseDate
     */
    public Date getBeginExpenseReimburseDate() {
        return beginExpenseReimburseDate;
    }

    /**
     * @return the endExpenseReimburseDate
     */
    public Date getEndExpenseReimburseDate() {
        return endExpenseReimburseDate;
    }

    /**
     * @return the beginLeavingDate
     */
    public Date getBeginLeavingDate() {
        return beginLeavingDate;
    }

    /**
     * @return the endLeavingDate
     */
    public Date getEndLeavingDate() {
        return endLeavingDate;
    }

    /**
     * @return the mainDriverId
     */
    public Long getMainDriverId() {
        return mainDriverId;
    }

    /**
     * @return the firstViceDriverId
     */
    public Long getFirstViceDriverId() {
        return firstViceDriverId;
    }

    /**
     * @return the expenseReimbursed
     */
    public Boolean getExpenseReimbursed() {
        return expenseReimbursed;
    }
    
    /**
     * @return the codeSort
     */
    public String getCodeSort() {
        return codeSort;
    }
    
    /**
     * @return
     */
    public String getLeavingTimeSort() {
        return leavingTimeSort;
    }

    /**
     * @return the dispatchDateSort
     */
    public String getDispatchDateSort() {
        return dispatchDateSort;
    }

    /**
     * @return the shipmentGroupIdSort
     */
    public String getShipmentGroupIdSort() {
        return shipmentGroupIdSort;
    }
    
    /**
     * @return
     */
    public String getTarget() {
        return target;
    }

    /**
     * @param organizationIds the organizationIds to set
     */
    public void setOrganizationIds(List<Long> organizationIds) {
        this.organizationIds = organizationIds;
    }

    /**
     * @param dispatchOrderCode the dispatchOrderCode to set
     */
    public void setDispatchOrderCode(String dispatchOrderCode) {
        this.dispatchOrderCode = StringUtils.trim(dispatchOrderCode);
    }

    /**
     * @param vehicleId the vehicleId to set
     */
    public void setVehicleId(Long vehicleId) {
        this.vehicleId = vehicleId;
    }

    /**
     * @param shipmentGroupId the shipmentGroupId to set
     */
    public void setShipmentGroupId(String shipmentGroupId) {
        this.shipmentGroupId = shipmentGroupId;
    }

    /**
     * @param startFacilityId the startFacilityId to set
     */
    public void setStartFacilityId(Long startFacilityId) {
        this.startFacilityId = startFacilityId;
    }

    /**
     * @param destinationFacilityId the destinationFacilityId to set
     */
    public void setDestinationFacilityId(Long destinationFacilityId) {
        this.destinationFacilityId = destinationFacilityId;
    }

    /**
     * @param beginDispatchDate the beginDispatchDate to set
     */
    public void setBeginDispatchDate(Date beginDispatchDate) {
        this.beginDispatchDate = beginDispatchDate;
    }

    /**
     * @param endDispatchDate the endDispatchDate to set
     */
    public void setEndDispatchDate(Date endDispatchDate) {
        this.endDispatchDate = endDispatchDate;
    }

    /**
     * @param beginExpenseReimburseDate the beginExpenseReimburseDate to set
     */
    public void setBeginExpenseReimburseDate(Date beginExpenseReimburseDate) {
        this.beginExpenseReimburseDate = beginExpenseReimburseDate;
    }

    /**
     * @param endExpenseReimburseDate the endExpenseReimburseDate to set
     */
    public void setEndExpenseReimburseDate(Date endExpenseReimburseDate) {
        this.endExpenseReimburseDate = endExpenseReimburseDate;
    }

    /**
     * @param beginLeavingDate the beginLeavingDate to set
     */
    public void setBeginLeavingDate(Date beginLeavingDate) {
        this.beginLeavingDate = beginLeavingDate;
    }

    /**
     * @param endLeavingDate the endLeavingDate to set
     */
    public void setEndLeavingDate(Date endLeavingDate) {
        this.endLeavingDate = endLeavingDate;
    }

    /**
     * @param mainDriverId the mainDriverId to set
     */
    public void setMainDriverId(Long mainDriverId) {
        this.mainDriverId = mainDriverId;
    }

    /**
     * @param firstViceDriverId the firstViceDriverId to set
     */
    public void setFirstViceDriverId(Long firstViceDriverId) {
        this.firstViceDriverId = firstViceDriverId;
    }

    /**
     * @param expenseReimbursed the expenseReimbursed to set
     */
    public void setExpenseReimbursed(Boolean expenseReimbursed) {
        this.expenseReimbursed = expenseReimbursed;
    }

    /**
     * @param codeSort the codeSort to set
     */
    public void setCodeSort(String codeSort) {
        this.codeSort = codeSort;
    }
    
    /**
     * @param leavingTimeSort
     */
    public void setLeavingTimeSort(String leavingTimeSort) {
        this.leavingTimeSort = leavingTimeSort;
    }

    /**
     * @param dispatchDateSort the dispatchDateSort to set
     */
    public void setDispatchDateSort(String dispatchDateSort) {
        this.dispatchDateSort = dispatchDateSort;
    }

    /**
     * @param shipmentGroupIdSort the shipmentGroupIdSort to set
     */
    public void setShipmentGroupIdSort(String shipmentGroupIdSort) {
        this.shipmentGroupIdSort = shipmentGroupIdSort;
    }

    /**
     * @param target
     */
    public void setTarget(String target) {
        this.target = target;
    }
    
}
